package com.ph.shopping.facade.member.service.dbservice.facade.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.customenum.MemberEnum;
import com.ph.shopping.common.core.customenum.OuterResultEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.SmsCodeType;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.core.other.smssend.util.SmsSendUtil;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.rsa.MD5;
import com.ph.shopping.facade.customfactory.CustomBeanRule;
import com.ph.shopping.facade.customfactory.facade.ICustomBeanFactory;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.mapper.IcCardMapper;
import com.ph.shopping.facade.mapper.MemberMapper;
import com.ph.shopping.facade.member.dto.IcCradindInfoDto;
import com.ph.shopping.facade.member.dto.MemberCardInfoDto;
import com.ph.shopping.facade.member.dto.MemberIcCardInfoPageDto;
import com.ph.shopping.facade.member.dto.RegisterMemberDto;
import com.ph.shopping.facade.member.dto.UnbundMemberCardDto;
import com.ph.shopping.facade.member.entity.IcCardInfo;
import com.ph.shopping.facade.member.entity.IcMemberBind;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.service.ISmsCodeSendService;
import com.ph.shopping.facade.member.service.dbservice.facade.IMemberCardDbService;
import com.ph.shopping.facade.member.service.user.LoginRegisterCheckService;
import com.ph.shopping.facade.member.service.user.response.CheckResponse;
import com.ph.shopping.facade.member.service.user.response.Content;
import com.ph.shopping.facade.member.vo.MemberCardInfoVo;

/**
 * 
* @ClassName: MemberCardDbService  
* @Description: 会员卡数据服务  
* @author lijie  
* @date 2017年3月20日  
*
 */
@Service	
public class MemberCardDbService implements IMemberCardDbService{

	private static final Logger log = LoggerFactory.getLogger(MemberDbService.class);

	@Autowired
	private MemberMapper memeberMapper;
	
	@Autowired
	private IcCardMapper icCardMapper;
	
	@Autowired
	private ICustomBeanFactory<CustomBeanRule> customBeanFactory;
	/**
	 * 短信发送
	 */
	@Autowired
	private ISmsCodeSendService smsCodeService;
	/**
	 * 登录注册校验服务
	 */
	@Autowired
	private LoginRegisterCheckService checkService;
	
	@Override
	@Transactional
	public Result addMemberCard(IcCardInfo icInfo) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Map<String, Object> map = ContainerUtil.map();
		map.put("outerCode", icInfo.getOuterCode());
		int num = icCardMapper.selectIcCardCountInfoByCode(map);
		if (num > 0) {
			result.setMessage("条形码已存在");
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			return result;
		}
		map.clear();
		map.put("innerCode", icInfo.getInnerCode());
		int numo = icCardMapper.selectIcCardCountInfoByCode(map);
		if (numo > 0) {
			result.setMessage("IC卡号已存在");
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			return result;
		}
		icInfo.setIsDelete(MemberEnum.MEMBER_NORMAL.getCode());
		if (icInfo.getCreateTime() == null) {
			icInfo.setCreateTime(new Date());
		}
		icCardMapper.insert(icInfo);
		return DubboResult.getResultBySuccess(result);
	}

	@Override
	@Transactional
	public Result bindMemberCard(IcCradindInfoDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Map<String, Object> map = ContainerUtil.map();
		map.put("outerCode", dto.getOuterCode());
		map.put("innerCode", dto.getInnerCode());
		map.put("isDelete", MemberEnum.CARD_NORMAL.getCode());
		List<IcCardInfo> infos = icCardMapper.selectIcCardInfoByCode(map);
		if (!ParamVerifyUtil.objIsNotNull(infos)) {
			result.setMessage("会员卡不存在或处于冻结状态");
			result.setCode(ResultEnum.MEMBER_NO_DATA.getCode());
			return result;
		} else if (verifyMemberCardIsBind(dto.getOuterCode(), dto.getInnerCode())) {
			result.setMessage("会员卡已被绑定");
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			return result;
		}
		Member member = memeberMapper.selectMemberInfoByMobile(dto.getPhone());
		log.info("绑定会员卡的会员数据：Member = {}", JSON.toJSONString(member));
		String pwd = null;
		boolean flag = false;
		if (flag = (member != null && StringUtils.isNotBlank(member.getMemberPhone()))) {
			Byte status = member.getStatus();
			if (MemberEnum.MEMBER_FROZEN.getCode().equals(status)) {
				result.setMessage("当前会员处于冻结状态");
				result.setCode(ResultEnum.MEMBER_FROZEN.getCode());
				return result;
			}
		} else {
			// 判断北京方是否已存在会员数据，存在则直接返回成功
			member = backOuterData(dto.getPhone(), null);
			flag = member != null && StringUtils.isNotBlank(member.getMemberPhone());
			if (!flag) {// true 表示北京方已存在会员数据
				result = getMemberByPhone(dto);
				if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
					Object data = result.getData();
					if (data instanceof Map) {
						@SuppressWarnings({ "unchecked", "rawtypes" })
						Map<String, Object> retMap = (Map) result.getData();
						Object mobj = retMap.get("member");
						if (mobj instanceof Member) {
							member = (Member) mobj;
						}
						Object pwdobj = retMap.get("password");
						if (pwdobj != null) {
							pwd = pwdobj.toString();
						}
					}
				}
			}
		}
		if (member == null || StringUtils.isBlank(member.getMemberPhone())) {
			throw new BizException("未获取到会员相关数据，不能进行绑卡操作");
		}
		IcCardInfo info = infos.get(0);
		result = bindMemberCardByInfo(info, member.getId(), dto.getMerchantId());
		if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
			// 绑定会员卡成功给用户发送短信 ，flag 为true 时表示数据已存在 不需要发送短信密码
			if (!flag) {
				if (sendSmsPwdCode(member.getMemberPhone(), pwd)) {
					return DubboResult.getResultBySuccess();
				}
			}
		}
		return result;
	}
	/**
	 * 
	* @Title: backOuterData  
	* @Description:处理同步北京调用方的会员数据  
	* @param @param phone
	* @param @return    参数  
	* @return Member    返回类型  
	* @throws
	 */
	private Member backOuterData(String phone, Byte level) {
		Member member = null;
		CheckResponse response = checkService.registerCheck(phone);
		if (response != null && OuterResultEnum.SUCCESS.getCode().equals(response.getCode())) {
			Content content = response.getContent();
			if (content != null && StringUtils.isNotBlank(content.getPhone())) {
				member = new Member();
				member.setCreateTime(new Date());
				member.setIdCard(content.getCard());
				member.setMemberName(content.getName());
				member.setMemberPhone(content.getPhone());
				member.setStatus(MemberEnum.MEMBER_NORMAL.getCode());
				member.setCertification(MemberEnum.NO_CERTIFICATION.getCode());
				if (level == null) {
					member.setLevel(MemberEnum.MEMBER_GENERAL.getCode());
				} else {
					member.setLevel(level);
				}
				memeberMapper.insertMember(member);
				Long memberId = member.getId();
				// 向会员积分表新增数据
				boolean flag = insertIntoScoreData(memberId);
				if (!flag) {
					throw new BizException("新增会员时向会员积分表新增数据失败");
				}
				member.setId(memberId);
			}
		}
		return member;
	}
	/**
	 * 
	* @Title: sendSmsPwdCode  
	* @Description: 发送短信
	* @param @param phone
	* @param @param pwd
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private boolean sendSmsPwdCode(String phone, String pwd) {
		Result result = smsCodeService.sendSmsCodeByCustomMsg(phone, SmsCodeType.SEND_USER_PWD.getCodeType(), pwd);
		if (result == null || !ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
			log.info("给用户发送短信密码失败");
			throw new BizException("给用户发送短信密码失败");
		}
		return true;
	}
	/**
	 * 
	* @Title: addMember  
	* @Description: 绑定会员卡时 会员不存在则新增会员数据
	* @param @param dto
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	@Transactional
	private Result addMember(RegisterMemberDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);

		String pwd = SmsSendUtil.getSmsCode();
		Member mem = memeberMapper.selectMemberInfoByMobile(dto.getMeber().getMemberPhone());
		if (mem != null && StringUtils.isNotBlank(mem.getMemberPhone())) {
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
			result.setMessage("当前会员已存在，不能重复添加");
			return result;
		}
		Member member = dto.getMeber();
		member.setMemberPwd(pwdMd5Str(pwd));
		member.setStatus(MemberEnum.MEMBER_NORMAL.getCode());
		member.setCertification(MemberEnum.NO_CERTIFICATION.getCode());
		// 默认为会员状态
		member.setIsMarketing(MemberEnum.IS_MARKETING_BYNO.getCode());
		if (member.getLevel() == null) {
			member.setLevel(MemberEnum.MEMBER_GENERAL.getCode());
		}
		memeberMapper.insertMember(member);
		Long memberId = member.getId();
		// 向会员积分表新增数据
		boolean flag = insertIntoScoreData(memberId);
		if (!flag) {
			throw new BizException("新增会员时向会员积分表新增数据失败");
		}
		result.setData(pwd);// 将新增的密码返回 提供给需要密码的取值
		return DubboResult.getResultBySuccess(result);
	}
	/**
	 * 
	* @Title: pwdMd5Str  
	* @Description: 密码加密
	* @param @param pwd
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	private String pwdMd5Str(String pwd){
		
		return MD5.getMD5Str(pwd);
	}
	/**
	 * 
	* @Title: insertIntoScoreData  
	* @Description: 新增会员时 向会员积分数据表新增记录 
	* @param @param memberId
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private boolean insertIntoScoreData(Long memberId) {
		if (memberId != null) {
			Map<String, Object> scoreMap = ContainerUtil.map();
			scoreMap.put("memberId", memberId);
			scoreMap.put("createTime", new Date());
			scoreMap.put("updateTime", new Date());
			memeberMapper.insertMemberScoreData(scoreMap);
			return true;
		}
		return false;
	}
	/**
	 * 
	* @Title: bindMemberCardByInfo  
	* @Description: 绑定会员卡
	* @param @param info
	* @param @param memberId
	* @param @param merchantId
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	private Result bindMemberCardByInfo(IcCardInfo info, Long memberId, Long merchantId) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		IcMemberBind icmember = new IcMemberBind();
		icmember.setMemberId(memberId);
		icmember.setStatus(MemberEnum.CARD_BOUND.getCode());
		icmember = icCardMapper.selectMemberCarBind(icmember);
		if (icmember != null && icmember.getId() != null) {
			result.setMessage("会员已绑定会员卡，不能重复绑定");
			result.setCode(ResultEnum.MEMBER_DATA_EXISTS.getCode());
		} else {
			// 绑定会员卡操作
			if (bindIcCard(info.getId(), memberId, merchantId)) {
				DubboResult.getResultBySuccess(result);
			}
		}
		return result;
	}
	/**
	 * 
	* @Title: verifyMemberCardIsBind  
	* @Description:判断会员卡是否被绑定 true 为已绑定
	* @param @param outerCode
	* @param @param innerCode
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	private boolean verifyMemberCardIsBind(String outerCode,String innerCode){
		Map<String, Object> map = ContainerUtil.map();
		map.put("outerCode", outerCode);
		map.put("innerCode", innerCode);
		map.put("status", MemberEnum.CARD_BOUND.getCode());
		int num = icCardMapper.selectMemberCardBindNum(map);
		return num > 0;
	}
	/**
	 * 
	* @Title: bindIcCard  
	* @Description: 绑定会员卡操作  
	* @param @param icCardId
	* @param @param memberId
	* @param @param merchantId
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	@Transactional
	private boolean bindIcCard(Long icCardId, Long memberId, Long merchantId) {
		IcMemberBind memberBind = new IcMemberBind();
		memberBind.setCreateTime(new Date());
		memberBind.setIcCardId(icCardId);
		memberBind.setMemberId(memberId);
		memberBind.setMerchantId(merchantId);
		memberBind.setStatus(MemberEnum.CARD_BOUND.getCode());
		memberBind.setOperatorUserId(merchantId);
		icCardMapper.insertIcMemberBind(memberBind);
		return true;
	}
	/**
	 * 
	* @Title: getMemberByPhone  
	* @Description: 根据添加会员卡的数据 查询会员信息， 
	* 存在则返回数据， 不存在则新增  
	* @param @param dto
	* @param @return    参数  
	* @return Member    返回类型  
	* @throws
	 */
	@Transactional
	private Result getMemberByPhone(IcCradindInfoDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		Member member = memeberMapper.selectMemberInfoByMobile(dto.getPhone());
		if (member == null || member.getId() == null) {
			RegisterMemberDto rdto = getRegisterMemberDto(dto);
			result = addMember(rdto);
			if (result == null || !ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
				throw new BizException("新增会员数据失败");
			}
			member = memeberMapper.selectMemberInfoByMobile(dto.getPhone());
			Object pwd = result.getData();
			Map<String, Object> retMap = ContainerUtil.map();
			retMap.put("password", pwd);
			retMap.put("member", member);
			result.setData(retMap);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}
	
	private RegisterMemberDto getRegisterMemberDto(IcCradindInfoDto dto) {
		RegisterMemberDto rdto = null;
		if (dto != null) {
			rdto = new RegisterMemberDto();
			Member member = new Member();
			member.setMemberPhone(dto.getPhone());
			member.setMemberName(dto.getName());
			rdto.setMeber(member);
		}
		return rdto;
	}

	@Override
	@Transactional
	public Result cancelMemberCard(UnbundMemberCardDto dto) {
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		String phone = dto.getPhone();
		Member member = memeberMapper.selectMemberInfoByMobile(dto.getPhone());
		if (member == null || member.getId() == null) {
			log.info("根据手机号：" + phone + " 没有找到相关会员消息");
			result.setCode(ResultEnum.MEMBER_NO_DATA.getCode());
			result.setMessage("根据手机号：" + phone + " 没有找到相关会员消息");
		} else {
			Byte status = member.getStatus();
			if (MemberEnum.MEMBER_FROZEN.getCode().equals(status)) {
				result.setMessage("当前会员处于冻结状态");
				result.setCode(ResultEnum.MEMBER_FROZEN.getCode());
				return result;
			}
			IcMemberBind icmember = new IcMemberBind();
			icmember.setMemberId(member.getId());
			icmember.setStatus(MemberEnum.CARD_BOUND.getCode());
			icmember.setMerchantId(dto.getOperatorUserId());
			icmember = icCardMapper.selectMemberCarBind(icmember);
			if (icmember != null && icmember.getId() != null) {
				// 挂失会员卡
				if (canlIcMemberBind(icmember)) {
					return DubboResult.getResultBySuccess();
				}
			} else {
				log.info("根据手机号：" + phone + " 没有找到会员绑定的会员卡消息");
				result.setCode(ResultEnum.MEMBER_NO_DATA.getCode());
				result.setMessage("根据手机号：" + phone + " 没有找到会员绑定的会员卡消息");
			}
		}
		return result;
	}

	/**
	 * 
	* @Title: canlIcMemberBind  
	* @Description: 挂失会员卡  
	* @param @param icmember
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	@Transactional
	private boolean canlIcMemberBind(IcMemberBind icmember) {

		icmember.setStatus(MemberEnum.CARD_LOSS.getCode());
		icmember.setReportTime(new Date());
		icmember.setUpdateTime(new Date());
		icCardMapper.updateMemeberIcCardBind(icmember);
		// 挂失后删除卡片信息
		Long icCardId = icmember.getIcCardId();
		if (icCardId != null) {
			IcCardInfo info = new IcCardInfo();
			info.setUpdateTime(new Date());
			info.setId(icCardId);
			info.setIsDelete(MemberEnum.CARD_LOSS.getCode());
			icCardMapper.updateByPrimaryKeySelective(info);
		}
		return true;
	}

	@Override
	public Result queryMemberBindCardByPage(MemberIcCardInfoPageDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = null;
		try {
			map = customBeanFactory.beanToMap(dto);
		} catch (Exception e) {
			log.error("MemberIcCardInfoPageDto to map error", e);
		}
		if(map != null){
			PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
			List<MemberCardInfoVo> list = icCardMapper.selectMemberBindCardByPage(map);
			PageInfo<MemberCardInfoVo> page = new PageInfo<MemberCardInfoVo>(list);
			result.setCount(page.getTotal());
			result.setData(page.getList());
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result queryMemberIcCardInfoByPhone(String Phone) {
		Result result = DubboResult.getDefResultByImpl();
		List<MemberCardInfoVo> list = icCardMapper.selectMemberBindCardInfoByPhone(Phone);
		if (ParamVerifyUtil.objIsNotNull(list)) {
			result.setData(list.get(0));
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result getMemberBindCardByPageAndAll(MemberIcCardInfoPageDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = null;
		try {
			map = customBeanFactory.beanToMap(dto);
		} catch (Exception e) {
			log.error("MemberIcCardInfoPageDto to map error", e);
		}
		if (map != null) {
			List<MemberCardInfoVo> list = icCardMapper.selectMemberBindCardByPage(map);
			PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
			PageInfo<MemberCardInfoVo> page = new PageInfo<MemberCardInfoVo>(list);
			result.setCount(page.getTotal());
			result.setData(page.getList());
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result frozenMemberCard(UnbundMemberCardDto dto) {
		IcCardInfo cradInfo = new IcCardInfo();
		cradInfo.setId(dto.getIcCardId());
		cradInfo.setIsDelete(MemberEnum.CARD_FROZEN.getCode());
		cradInfo.setUpdateTime(new Date());
		// 冻结会员卡
		icCardMapper.updateByPrimaryKeySelective(cradInfo);
		// 修改绑定关系
		IcMemberBind memberBind = new IcMemberBind();
		memberBind.setIcCardId(dto.getIcCardId());
		memberBind.setOperatorUserId(dto.getOperatorUserId());
		memberBind.setUpdateTime(new Date());
		memberBind.setStatus(MemberEnum.CARD_FROZEN.getCode());
		icCardMapper.updateCardBindInfoByIcId(memberBind);
		return DubboResult.getResultBySuccess();
	}

	@Override
	public Result queryMemberCardByPage(MemberIcCardInfoPageDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = null;
		try {
			map = customBeanFactory.beanToMap(dto);
		} catch (Exception e) {
			log.error("MemberIcCardInfoPageDto to map error", e);
		}
		if (map != null) {
			PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
			List<MemberCardInfoVo> list = icCardMapper.selectMemberCardByPage(map);
			PageInfo<MemberCardInfoVo> page = new PageInfo<MemberCardInfoVo>(list);
			result.setCount(page.getTotal());
			result.setData(page.getList());
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	@Transactional
	public Result relieveMemberCard(UnbundMemberCardDto dto) {
		IcCardInfo cradInfo = new IcCardInfo();
		cradInfo.setId(dto.getIcCardId());
		cradInfo.setIsDelete(MemberEnum.CARD_NORMAL.getCode());
		cradInfo.setUpdateTime(new Date());
		// 解冻会员卡
		icCardMapper.updateByPrimaryKeySelective(cradInfo);
		// 修改绑定关系
		IcMemberBind memberBind = new IcMemberBind();
		memberBind.setIcCardId(dto.getIcCardId());
		memberBind.setOperatorUserId(dto.getOperatorUserId());
		memberBind.setUpdateTime(new Date());
		memberBind.setStatus(MemberEnum.CARD_BOUND.getCode());
		icCardMapper.updateCardBindInfoByIcId(memberBind);
		return DubboResult.getResultBySuccess();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Result batchAddMemberCard(List<IcCardInfo> list) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> ois = getOuterAndInner(list);
		if (ois != null && !ois.isEmpty()) {
			List<IcCardInfo> backList = ContainerUtil.lList();
			List<String> iCodes = (List<String>) ois.get("innerCodes");
			List<String> oCodes = (List<String>) ois.get("outerCodes");
			List<IcCardInfo> repeatCodes = (List<IcCardInfo>) ois.get("repeatCodes");
			if (ParamVerifyUtil.objIsNotNull(repeatCodes)) {
				backList.addAll(repeatCodes);
			}
			Map<String, List<IcCardInfo>> retMap = null;
			if (ParamVerifyUtil.objIsNotNull(iCodes)) {
				List<String> innerCodes = icCardMapper.selectIcinnerCodeByInnerCodes(iCodes);
				retMap = getIcCradindInfoDtos(innerCodes, list, "innerCode");
				if (retMap != null) {
					list = retMap.get("noExists");
					List<IcCardInfo> existList = retMap.get("exists");
					if (ParamVerifyUtil.objIsNotNull(existList)) {
						backList.addAll(existList);
					}
				}
			}
			if (ParamVerifyUtil.objIsNotNull(oCodes)) {
				List<String> outerCodes = icCardMapper.selectIcOuterCodeByOuterCodes(iCodes);
				retMap = getIcCradindInfoDtos(outerCodes, list, "outerCode");
				if (retMap != null) {
					list = retMap.get("noExists");
					List<IcCardInfo> existList = retMap.get("exists");
					if (ParamVerifyUtil.objIsNotNull(existList)) {
						backList.addAll(existList);
					}
				}
			}
			// 执行批量新增操作
			if (ParamVerifyUtil.objIsNotNull(list)) {
				if (batchInsertCard(list)) {
					result.setData(backList);
					result = DubboResult.getResultBySuccess(result);
				}
			} else {
				result = DubboResult.getResultByEnum(ResultEnum.MEMBER_DATA_EXISTS);
			}
		}
		return result;
	}
	/**
	 * 
	* @Title: batchInsertCard  
	* @Description: 批量信息会员卡
	* @param @param list
	* @param @return    参数  
	* @return boolean    返回类型  
	* @throws
	 */
	@Transactional
	private boolean batchInsertCard(List<IcCardInfo> list) {
		if (ParamVerifyUtil.objIsNotNull(list)) {
			icCardMapper.batchInsertCard(list);
			return true;
		}
		return false;
	}
	/**
	 * 
	* @Title: getIcCradindInfoDtos  
	* @Description: 移除相同的数据 
	* @param @param codes
	* @param @param list
	* @param @param type
	* @param @return    参数  
	* @return Map<String,List<IcCradindInfoDto>>    返回类型  
	* @throws
	 */
	private Map<String, List<IcCardInfo>> getIcCradindInfoDtos(List<String> codes, List<IcCardInfo> list, String type) {
		if (!ParamVerifyUtil.objIsNotNull(codes) || !ParamVerifyUtil.objIsNotNull(list)) {
			return null;
		}
		Map<String, List<IcCardInfo>> map = ContainerUtil.map();
		List<IcCardInfo> existsList = ContainerUtil.lList();
		Iterator<IcCardInfo> itor = list.iterator();
		while (itor.hasNext()) {
			IcCardInfo dto = itor.next();
			if ("outerCode".equals(type)) {
				if (codes.contains(dto.getOuterCode())) {
					existsList.add(dto);
					itor.remove();
					continue;
				}
			} else if ("innerCode".equals(type)) {
				if (codes.contains(dto.getInnerCode())) {
					existsList.add(dto);
					itor.remove();
					continue;
				}
			}
		}
		map.put("exists", existsList);
		map.put("noExists", list);
		return map;
	}
	/**
	 * 
	* @Title: getOuterAndInner  
	* @Description: 将新增的会员卡 内码和外码 分开存储
	* （备注：此处会移除重复的数据，会改变传入集合的数据，没有做返回处理）
	* @param @param list 
	* @param @return    参数  
	* @return Map<String,List<String>>    返回类型  
	* @throws
	 */
	private Map<String, Object> getOuterAndInner(List<IcCardInfo> list) {
		Map<String, Object> map = null;
		if (ParamVerifyUtil.objIsNotNull(list)) {
			map = ContainerUtil.map();
			Set<String> os = ContainerUtil.set();
			Set<String> is = ContainerUtil.set();
			// excel重复的数据
			List<IcCardInfo> repeatCodes = ContainerUtil.lList();
			Iterator<IcCardInfo> itor = list.iterator();
			while (itor.hasNext()) {
				IcCardInfo info = itor.next();
				if (os.contains(info.getOuterCode())) {
					repeatCodes.add(info);
					itor.remove();
					continue;
				}
				if (is.contains(info.getInnerCode())) {
					repeatCodes.add(info);
					itor.remove();
					continue;
				}
				info.setCreateTime(new Date());
				info.setIsDelete(MemberEnum.CARD_NORMAL.getCode());
				os.add(info.getOuterCode());
				is.add(info.getInnerCode());
			}
			map.put("outerCodes", ContainerUtil.aList(os));// 需要导入的外码
			map.put("innerCodes", ContainerUtil.aList(is));// 需要导入的内码
			map.put("repeatCodes", repeatCodes);// excel 重复的数据
		}
		return map;
	}

	@Override
	public Result queryMemberCardInfoByCode(IcCradindInfoDto dto) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = ContainerUtil.map();
		map.put("innerCode", dto.getInnerCode());
		map.put("outerCode", dto.getOuterCode());
		MemberCardInfoDto mCard = icCardMapper.selectMemberCardInfoByCode(map);
		if(mCard != null){
			result.setData(mCard);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result queryMemberCardInfoByInnerCode(String innerCode) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = ContainerUtil.map();
		map.put("innerCode", innerCode);
		MemberCardInfoDto mCard = icCardMapper.selectMemberCardInfoByCode(map);
		if(mCard != null){
			result.setData(mCard);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result queryMemberCardInfoByOuterCode(String outerCode) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = ContainerUtil.map();
		map.put("outerCode", outerCode);
		MemberCardInfoDto mCard = icCardMapper.selectMemberCardInfoByCode(map);
		if(mCard != null){
			result.setData(mCard);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}
}
